import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/generator/login'
import Exhibition from '@/views/generator/exhibition'
import Exhibits from '@/views/generator/exhibits'
import CulturalProduct from '@/views/generator/culturalproduct'
import Ticket from '@/views/generator/tickets'
import Order from '@/views/generator/orders'
import Administor from '@/views/generator/administor'
import Visitor from '@/views/generator/visitor'
import Main from '@/views/generator/Main'
import isLoginJs from '@/utils/isLogin'
// import { MessageBox, Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/page',
    name: 'page',
    component: Main,
    redirect: '/exhibition',
    children: [
      {
        path: '/exhibition',
        // name: 'exhibition',
        component: Exhibition
      },
      {
        path: '/exhibit',
        name: 'exhibit',
        component: Exhibits
      },
      {
        path: '/culpro',
        name: 'culpro',
        component: CulturalProduct
      },
      {
        path: '/ticket',
        name: 'ticket',
        component: Ticket
      },
      {
        path: '/order',
        name: 'order',
        component: Order
      },
      {
        path: '/administor',
        name: 'administor',
        component: Administor
      },
      {
        path: '/visitor',
        name: 'visitor',
        component: Visitor
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  // console.log('是否需要登陆才能访问')
  // 跳转路径为登录页，直接放行
  if (to.path === '/') {
    next()
  } else { // 需要登陆才能使用的页面
    if (!isLoginJs.isLogin()) { // 先登录才能访问
    // console.log('请先进行登录')
      alert('请先进行登录')
      // MessageBox.alert('请先进行登陆', '警告', {
      //   confirmButtonText: '确定',
      //   callback: action => {
      //     Message({
      //       type: 'info',
      //       message: `action: ${action}`
      //     })
      //   }
      // })
      next({
        path: '/'
      })
    } else { // 此时已经登陆了
      next()
    }
  }
})

export default router
